<?php
require_once 'Zend/Log.php';

class Cts_Log
{
	protected $logger;
	
	public function __construct( $config )
	{
		// writer
		$writer = new Zend_Log_Writer_Stream( $config->log->file ) ;
		
		// formatter
		$format = '%timestamp% %priorityName% (%priority%): URI(%uri%) %class%->%func%() %message%' . PHP_EOL ;
		$formatter = new Zend_Log_Formatter_Simple( $format ) ;
		$writer->setFormatter( $formatter ) ;

		$this->logger = new Zend_Log( $writer ) ;
		
		$filter = new Zend_Log_Filter_Priority( intval( $config->log->level ) ) ;
		$this->logger->addFilter( $filter ) ;
		
		$this->logger->setEventItem( 'uri' , $_SERVER['REQUEST_URI'] ) ;
		$this->setClass( 'null' ) ;
		$this->setFunc( 'null' ) ;
	}
		
	public function setClass( $class )
	{
		$this->logger->setEventItem( 'class' , $class ) ;
	}
	
	public function setFunc( $func )
	{
		$this->logger->setEventItem( 'func' , $func ) ;
	}
	
	public function debug( $msg , $func = null )
	{
		if( !empty( $func ) )
			$this->setFunc( $func ) ;
		$this->logger->debug( $msg ) ;
	}
	
	public function info( $msg , $func = null )
	{
		if( !empty( $func ) )
			$this->setFunc( $func ) ;
		$this->logger->info( $msg ) ;
	}
	
	public function warn( $msg , $func = null )
	{
		if( !empty( $func ) )
			$this->setFunc( $func ) ;
		$this->logger->warn( $msg ) ;
	}
	
	public function err( $msg , $func = null )
	{
		if( !empty( $func ) )
			$this->setFunc( $func ) ;
		$this->logger->err( $msg ) ;
	}
	
	public function crit( $msg , $func = null )
	{
		if( !empty( $func ) )
			$this->setFunc( $func ) ;
		$this->logger->crit( $msg ) ;
	}
}